package com.jwp.util;

import java.util.List;

import org.apache.http.cookie.Cookie;
import org.apache.log4j.Logger;

/**
 * Cookie的操作类
 * @date 2009-04-29
 * @version 1.0
 */
public class CookieUtil {
	private static Logger log = Logger.getLogger(CookieUtil.class);

	/**
	 * 对Cookie数组进行循环遍历
	 * @param cookies 要遍历的Cookie数组
	 */
	public static void showCookie(Cookie[] cookies) {
		if (cookies == null)
			return;
		for (Cookie c : cookies) {
			log.info(c);
		}
	}

	/**
	 * 把Cookie数组转化成字符串
	 * 		以分号隔开
	 * @param cookies
	 * @return
	 */
	public static String parseCookies2String(Cookie[] cookies) {
		if (cookies == null)
			return "";

		StringBuilder cookieValue = new StringBuilder();
		for (Cookie c : cookies) {
			cookieValue.append(c);
			cookieValue.append(";");
		}
		return cookieValue.toString();

	}

	/**
	 * 对Cookie集合进行循环遍历
	 * @param cookies 要遍历的Cookie集合
	 */
	public static void showCookie(List<Cookie> cookies) {
		if (cookies == null)
			return;
		for (Cookie c : cookies) {
			System.out.println(c);
		}
	}

	/**
	 * 把Cookie集合转化成字符串
	 * 		以分号隔开
	 * @param cookies
	 * @return
	 */
	public static String parseCookies2String(List<Cookie> cookies) {
		if (cookies == null)
			return "";

		StringBuilder cookieValue = new StringBuilder();
		for (Cookie c : cookies) {
			cookieValue.append(c);
			cookieValue.append(";");
		}
		return cookieValue.toString();

	}
}
